草庐IT

android - @Subscribe 方法为同一事件调用多次

全部标签

ruby-on-rails - 如何在 Ruby 中访问私有(private)类方法?

给定一个Ruby类:classMyClassdefself.my_class_methodputs"classmethod"endprivatedefmy_methodputs"regularmethod"endprivate_class_method:my_class_methodend要访问私有(private)方法,我可以在类对象上调用.send(:my_method),但这对类方法有何作用? 最佳答案 你应该这样做:classMyClassdefself.my_class_methodputs"classmethod"end

ruby - RSpec:期望引发错误的方法失败

我正在尝试测试在特定条件下是否正确引发错误。在此规范中,出现了错误,但测试仍然失败。我做错了什么?require'spec_helper'describeUSBTeensyRendererdocontext'whenthecorrectUSBportnameisnotpresent'doit'raisesanerroroninstantiation'doexpect(renderer=USBTeensyRenderer.new).toraise_error(USBInitError)endendend以及“bundleexecrspec”的终端输出:Failures:1)USBTeen

ruby - 在 ruby​​ 中将类方法转换为 proc 的惯用方法

假设我想使用Proc描述Kernel.puts。我该怎么做?我能想到很多可能性;Proc.newdo|*args|Kernel.puts*argsend:puts.to_proc.curry[Kernel]#doesn'twork,returns`nil`asputsisvarargs但是两者都非常冗长。 最佳答案 method是您要找的吗?它可以让您将方法保存到变量。2.1.0:003>m=Kernel.method(:puts)=>#2.1.0:004>m.call('hi')hi

Ruby 方法和多个默认值的排序

我似乎无法做到这一点(我以前可以用Python做到这一点)。让我解释一下..假设我在Ruby中有以下方法:defsomeMethod(arg1=1,arg2=2,arg3=3).........end现在我可以调用这个方法someMethod(2,3,4)someMethod(2,3)someMethod(2)并且参数是按照它们各自的顺序获取的。但是如果我想在我的编程中的某个时刻给出arg2并且想要arg1和arg3的默认值怎么办?我尝试编写someMethod(arg2=4)但这在Ruby1.9中似乎不起作用。它所做的是它仍然认为arg1是4。在python中我至少可以摆脱这个,但在

ruby - == 是 Ruby 中的一个特殊方法吗?

我理解x==y在Ruby中解释为a.==(y)。我尝试检查是否可以使用自定义方法foo实现相同的效果,如下所示:classObjectdeffoo(n)self==nendendclassAattr_accessor:xenda=A.newa.x=4putsa.x.==(4)#=>trueputsa.x.foo(4)#=>trueputsa.x==4#=>trueputsa.xfoo4#=>in`x':wrongnumberofarguments(1for0)(ArgumentError)不幸的是,这不起作用。我错过了什么?==是Ruby中的一个特殊方法吗?

Ruby:自动加载方法有什么作用?

moduleActionControllerextendActiveSupport::Autoloadautoload:Baseautoload:Cachingautoload:Metalautoload:Middlewareend任何人都可以用示例/样本输出详细说明自动加载方法的作用吗? 最佳答案 Autoload确保在需要时自动加载类或模块。PeterCooper有一篇不错的文章,名为"RubyTechniquesRevealed:Autoload"这解释了需要的差异。我不想在这里重复他的例子:-)

ruby - 为 6 :Fixnum (Rails) in a view with jQuery + ERB? 获取未定义的方法 `gsub'

我正在尝试向特定View添加一些jQuery+ERB:views/posts/show.html.erb(文件顶部):$(".post-h3").prepend('');postsshow(etc...)">votestrue%>views/layouts/application.html.erb(文件底部):(etc...)但我收到以下错误:undefinedmethod`gsub'for6:FixnumExtractedsource(aroundline#3):1:2:3:$("post-").html('');4:5:有什么解决这个问题的建议吗? 最佳

ruby-on-rails - 提示需要散列的事件资源

我正在使用事件资源从api获取数据并显示它,我的Controllermodel.rb有classThr::Vol::Dom'/vv/test/domains/2013-06-25T05:03Z')xendend当我调用Thr::Vol::Dom.find方法时,它返回以下错误:ArgumentError:expectedanattributesHash,got["0.0.0.0","1.1.1.1","2.2.2.2","3.3.3.3","4.4.4.4"]该api应该提供这样的内容{"abs.com":["0.0.0.0","1.1.1.1","2.2.2.2","3.3.3.3"

ruby-on-rails - 从 rake 任务调用 Controller

我想从rake任务中调用Controller操作。我的问题是准备http请求的最佳方法是什么?感谢所有提示。编辑:有人有其他提示吗?我试过这个但没有用:controller_obj=Controller.newcontroller.your_method我遇到了这个异常:rakeaborted!uninitializedconstantController编辑2:我试过:sess=ActionController::Integration::Session.newsess.post('/route','codes=3')但是我得到了(我在rake文件中需要'action_control

ruby - 如何访问父/兄弟模块方法

有没有什么方法可以在classQux中访问baz_method而无需首先提及模块namespace?当有很多嵌套模块时,代码看起来不干净。moduleFoomoduleBarmoduleBazclassQuxdefself.qux_methodFoo::Bar::Baz.baz_methodendenddefself.baz_methodendendendend 最佳答案 常量首先在词法封闭模块中查找,然后在继承链中向上查找。moduleFoomoduleBarmoduleBazclassQuxdefself.qux_methodB